package com.distribution.system.controller;


import com.distribution.common.core.controller.BaseController;
import com.distribution.common.core.domain.AjaxResult;
import com.distribution.common.core.page.TableDataInfo;
import com.distribution.common.exception.ServiceException;
import com.distribution.common.utils.StringUtils;
import com.distribution.system.domain.converter.AuthenticationConverter;
import com.distribution.system.domain.dto.AuthInfoDto;
import com.distribution.system.domain.dto.AuthInfoListDto;
import com.distribution.system.domain.entity.FuluUploadPeople;
import com.distribution.system.domain.req.AuthInfoListReq;
import com.distribution.system.domain.req.ReqResult;
import com.distribution.system.service.FuluUploadPeopleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;


/**
 * (CmccDaCommissionReflection)表控制层
 * @since 2023-05-21 20:06:19
 */
@RestController
@RequestMapping(value = "/fuluUploadPeople")
@Slf4j
public class FuluUploadPeopleController extends BaseController {
    @Resource
    FuluUploadPeopleService fuluUploadPeopleService;
    @Resource
    AuthenticationConverter authenticationConverter;


    @PostMapping(value = "/fuluUploadPeople")
    public AjaxResult fuluUploadPeople(@RequestBody FuluUploadPeople fuluUploadPeople) {
        fuluUploadPeopleService.fuluUploadPeople(fuluUploadPeople);
        return success();
    }

    @PreAuthorize("@ss.hasAnyRoles('operationSupervisor')")
    @PostMapping(value = "/delAuthInfoById/{authInfoId}")
    public AjaxResult delAuthInfoById(@PathVariable int authInfoId) {
        fuluUploadPeopleService.removeById(authInfoId);
        return success();
    }

    @PreAuthorize("@ss.hasAnyRoles('operationSupervisor')")
    @PostMapping(value = "/updateAuthInfo")
    public ReqResult updateAuthInfo(@RequestBody @Validated AuthInfoDto authInfoDto) {
        if (Objects.isNull(authInfoDto.getId())) return ReqResult.error("修改失败，ID不能为空");
        FuluUploadPeople one = fuluUploadPeopleService.lambdaQuery().eq(FuluUploadPeople::getId, authInfoDto.getId()).one();
        if (one == null) return ReqResult.error("当前认证信息不存在");
        return fuluUploadPeopleService.updateAuthInfoById(authInfoDto);
    }

    @PostMapping(value = "/selectPeople")
    public AjaxResult selectPeople() {
        return success(fuluUploadPeopleService.selectPeople(null));
    }

    @PreAuthorize("@ss.hasAnyRoles('operationSupervisor')")
    @PostMapping(value = "/getAuthInfoList")
    public TableDataInfo getAuthInfoList(@RequestBody AuthInfoListReq authInfoListReq) {
        AuthInfoListDto authInfoListDto = authenticationConverter.authInfoListReqToDto(authInfoListReq);
        startPage();
        List<FuluUploadPeople> fuluUploadPepleInfoList = fuluUploadPeopleService.getFuluUploadPepleInfoList(authInfoListDto);
        return getDataTable(fuluUploadPepleInfoList);
    }

    @PostMapping(value = "/queryPeople")
    public AjaxResult queryPeople(@RequestBody FuluUploadPeople fuluUploadPeople) {
        String result = fuluUploadPeopleService.queryPeople(fuluUploadPeople);
        return success(result);
    }

    @PostMapping(value = "/selectPeopleByUserId/{userId}")
    public AjaxResult selectPeopleByUserId(@PathVariable Long userId) {
        return success(fuluUploadPeopleService.selectPeople(userId));
    }
}
